package main

import (
	"fmt"
	"time"

	ytask "github.com/gojuukaze/YTask/v2"
	"github.com/gojuukaze/YTask/v2/server"
)

type User struct {
	Id   int
	Name string
}

var client server.Client

func main() {
	// 对于client端你需要设置连接池大小
	broker := ytask.Broker.NewRedisBroker("127.0.0.1", "6379", "", 14, 5)

	// 对于client端，如果连接池<=0，poolSize会默认为10
	backend := ytask.Backend.NewRedisBackend("127.0.0.1", "6379", "", 15, 5)

	ser := ytask.Server.NewServer(
		ytask.Config.Broker(&broker),
		ytask.Config.Backend(&backend),
		ytask.Config.Debug(true),
		ytask.Config.StatusExpires(60*5),
		ytask.Config.ResultExpires(60*5),
	)

	client = ser.GetClient()

	for {
		// 提交任务
		taskId, _ := client.Send("group1", "add", 123, 44)
		// 获取结果
		result, _ := client.GetResult(taskId, 2*time.Second, 300*time.Millisecond)

		if result.IsSuccess() {
			// 有多种方法获取返回值，具体可以参考： https://doc.ikaze.cn/YTask/client.html#id4
			sum, _ := result.GetInt64(0)
			// or
			var sum2 int
			result.Get(0, &sum2)

			fmt.Println("add(123,44) =", int(sum))
		}

		// 提交结构体，slice等
		taskId, _ = client.Send("group1", "append_user", User{1, "aa"}, []int{322, 11}, []string{"bb", "cc"})
		result, _ = client.GetResult(taskId, 2*time.Second, 300*time.Millisecond)
		var users []User
		result.Get(0, &users)
		fmt.Println(users)

		time.Sleep(5 * time.Second)
	}

}
